package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AdChannel;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.AdThreadLocalUtil;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    /**
     * 查询所有频道
     *
     * @return
     */

    @Override
    public ResponseResult findALl() {
        return ResponseResult.okResult(list());
    }

    /**
     * 保存频道，管理员端和自媒体端都要添加
     *
     * @param channel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel channel) {
        //检查参数
        if(channel==null)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        //查询是否存在同名的频道，存在则不能保存
        WmChannel wmChannel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, channel.getName()));
        if(wmChannel!=null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已存在该频道");
        }
        if(channel.getIsDefault()==null)channel.setIsDefault(true);
        if(channel.getStatus()==null)channel.setStatus(true);
        channel.setCreatedTime(new Date());
        save(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询频道
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findListWithPage(ChannelDto dto) {
        //检查参数
        if(dto==null)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        //分页参数查检查
        dto.checkParam();
        try {
            //因为AdThread属于admin的服务，而这个实现类方法属于wemdia的服务，
            // 不同服务有不同的线程，所以无法从另一个服务中获取另一条线程的userid,
            // 只能通过请求头获取userid,并设置给WmThread，并从中获取
            WmUser user = WmThreadLocalUtil.getUser();
            log.info("user为:{}",user.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //分页查询
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //按照名字模糊查询
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(WmChannel::getName,dto.getName());
        }
        //按照创建时间倒序排序
        lambdaQueryWrapper.orderByDesc(WmChannel::getCreatedTime);
        page=page(page,lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        //结果返回
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 修改频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        //检查参数
        if(adChannel==null)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        //更新条件
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannel,wmChannel);
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delChannelById(Integer id) {
        //检查参数
        if(id==null)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        WmChannel wmChannel = new WmChannel();
        wmChannel.setId(id);
        //删除频道
        removeById(wmChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}